package com.sf.bank.impl;

import com.sf.bank.dao.UserDao;
import com.sf.bank.dao.UserMapper;
import com.sf.bank.entity.UserInfo;
import com.sf.bank.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Override
    @Cacheable("findAll")
    public List<UserInfo> findAll() {
        System.out.println("从Mysql中查询");
        List<UserInfo> list = this.userMapper.queryAll();
        return list;
    }

    @Override
//    @CacheEvict(value = "userCache", key = "#id")
    public List<UserInfo> queryUserByName(String name) {
        // 保存数据
        this.redisTemplate.boundValueOps("redis").set("Hello redis !");
        // 设置有效时间为100秒
        this.redisTemplate.boundValueOps("redis").expire(100l, TimeUnit.SECONDS);
        // 给value每次执行加一操作
        this.redisTemplate.boundValueOps("count").increment(1l);
        System.out.println("缓存清理了！");
        List<UserInfo> list = this.userMapper.queryUserByName(name);
        return list;
    }

    @Override
    @CacheEvict("findAll")
    public int insertUserInfo(UserInfo userInfo) {
        int insertFlg = userMapper.insertUserInfo(userInfo);
        return insertFlg;
    }

    /**
     * 验证用户登陆
     * @param userInfo
     * @return
     */
    @Override
    public List<UserInfo> userAuthLogin(UserInfo userInfo) {
        return userMapper.userAuthLogin(userInfo);
    }
}
